package com.ckm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ckm.entity.Classes;
import com.ckm.entity.Course;
import com.ckm.entity.Major;
import com.ckm.entity.TeacherCourse;
import com.ckm.exception.BusinessException;
import com.ckm.mapper.ClassesMapper;
import com.ckm.mapper.CourseMapper;
import com.ckm.mapper.MajorMapper;
import com.ckm.mapper.TeacherCourseMapper;
import com.ckm.service.ClassesService;
import com.ckm.util.FindObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ClassesServiceImpl implements ClassesService {

    @Autowired
    private ClassesMapper classesMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private MajorMapper majorMapper;
    @Autowired
    private TeacherCourseMapper teacherCourseMapper;

    @Override
    public void addClasses(List<Classes> classesList) {
        if (classesList == null || classesList.isEmpty()) {
            throw new BusinessException(250,"添加数据集为空");
        }
        for (Classes classes : classesList) {
            if (!findClasses(classes).isEmpty()) {
                throw new BusinessException(250,"数据重复:"+classes);
            }
            if (majorMapper.selectById(classes.getMajorId())==null){
                throw new BusinessException(250,"无法找到学院："+classes.getMajorId());
            }
            classesMapper.insert(classes);
        }
        log.info("添加数据：{}", classesList);
    }

    @Override
    public void deleteClasses(Integer id) {
        if (id==null){
            throw new BusinessException(250,"删除参数为空");
        }
        if (classesMapper.selectById(id)==null){
            throw new BusinessException(250,"无此班级");
        }
        classesMapper.deleteById(id);
    }

    @Override
    public void updateClasses(Classes classes) {
        if (classes.getId()==null){
            throw new BusinessException(250,"更新信息缺少id");
        }
        Classes select = classesMapper.selectById(classes.getId());
        if (select==null){
            throw new BusinessException(250,"无此班级");
        }
        if (select.equals(classes)){
            throw new BusinessException(250,"信息无更改");
        }
        classesMapper.updateById(classes);
    }

    @Override
    public PageInfo<Classes> getClassesPage(Classes classes, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(findClasses(classes));
    }

    @Override
    public List<Classes> findClasses(Classes classes) {
        QueryWrapper<Classes> wrapper = new QueryWrapper<>();
        FindObjectUtil.fullQueryWrapper(classes,wrapper);
        return classesMapper.selectList(wrapper);
    }

    @Override
    public List<Classes> findClassesByCourse(String courseName) {
        List<Course> courseList =
                courseMapper.selectList(new QueryWrapper<Course>()
                        .eq("course_name", courseName));
        if (courseList==null || courseList.isEmpty()){
            throw new BusinessException(250,"此课程不存在");
        }
        List<TeacherCourse> teacherCourseList = teacherCourseMapper.selectList(new QueryWrapper<TeacherCourse>()
                .eq("course_id", courseList.getFirst().getId()));
        if (teacherCourseList==null || teacherCourseList.isEmpty()){
            throw new BusinessException(250,"该课程尚未开课");
        }
        List<Integer> collect = teacherCourseList.stream().map(TeacherCourse::getClassId).toList();
        List<Classes> classesList = classesMapper.selectBatchIds(collect);
        if (classesList==null || classesList.isEmpty()){
            throw new BusinessException(250,"不存在班级上此课程");
        }
        return classesList;
    }

    @Override
    public String concatenateClassName(Integer classesId) {
        Classes classes = classesMapper.selectById(classesId);
        if (classes == null){
            throw new BusinessException(250,"查找不到"+classesId+"班级");
        }
        Major major = majorMapper.selectById(classes.getMajorId());
        if (major == null){
            throw new BusinessException(250,"找不到该班级所在专业");
        }
        return classes.getGrade() + "级" + major.getMajorName() + classes.getNum() + "班";
    }
}
